package com.jishaokang.service.impl;

import com.jishaokang.base.Result;
import com.jishaokang.cache.ResultCache;
import com.jishaokang.dao.CommentDAO;
import com.jishaokang.model.dto.Comment;
import com.jishaokang.model.vo.CommentVO;
import com.jishaokang.service.CommentService;
import com.jishaokang.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by NANA_Final on 2019/6/21.
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDAO commentDAO;
    @Autowired
    private TokenUtil tokenUtil;
    @Override
    public Result addComment(Comment comment) {
        int userId = tokenUtil.getUserIdByToken(comment.getToken());
        comment.setUserId(userId);
        commentDAO.insert(comment);
        return ResultCache.OK;
    }

    @Override
    public Result deleteComment(Comment comment) {
        commentDAO.delete(comment.getCommentId());
        return ResultCache.OK;
    }

    @Override
    public Result selectComment(Comment comment) {
        comment.setPage((comment.getPage()-1)*comment.getNumber());
        int total = commentDAO.selectTotal(comment);
        List<CommentVO> comments = commentDAO.select(comment);
        return ResultCache.getListDataOk(total,"comments",comments);
    }

}